package com.wemei.auto.ui.base

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import by.kirich1409.viewbindingdelegate.viewBinding
import com.wemei.auto.ui.ext.ACTION_USER_LOGIN
import com.wemei.auto.ui.helper.*
import com.wemei.auto.ui.utils.Logger
import com.wemei.auto.ui.utils.ReflectUtils

open class BaseFragment<VB : ViewBinding> : Fragment(), View.OnClickListener, IMsa by msa(), ILoa by loa() {
    val vb: VB by viewBinding(::loadViewBinding)
    protected lateinit var mThis: BaseFragment<VB>
    protected var mView: View? = null
    protected var mSavedInstanceState: Bundle? = null
    open val lazy: Boolean get() = lazyInitData
    open val lazyInitData: Boolean get() = false
    private var isFirstLoad = true

    private val dispatcher by lazy { requireActivity().onBackPressedDispatcher }
    private val callback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            onBackPressed()
        }
    }

    open fun loadViewBinding(fragment: Fragment): VB {
        val clz = ReflectUtils.findParameterizedTypeClz<VB>(this::class.java, 0)
        val method = clz.getDeclaredMethod("inflate", LayoutInflater::class.java)
        return method.invoke(null, layoutInflater) as VB
    }

    open fun loadView(load: VB.() -> Unit) = vb.also(load)

    fun onBackPressed() {
        callback.isEnabled = false
        dispatcher.onBackPressed()
    }

    fun hasExtras(): Boolean {
        return arguments != null
    }

    override fun getView(): View? {
        return mView
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mThis = this
        localRegisterReceiver(ACTION_USER_LOGIN, ::loginChange)
        initManageStartActivity()
        initLifecycleOwnerActivity()
    }

    open fun loginChange(intent: Intent) {

    }

    override fun onResume() {
        super.onResume()
        if (lazy && isFirstLoad) {
            isFirstLoad = false
            initView(mView, mSavedInstanceState)
            initData(mView, mSavedInstanceState)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        mView = vb.root
        viewLoad(mView)
        try {
            mSavedInstanceState = savedInstanceState
            if (!lazy) {
                isFirstLoad = false
                initView(mView, savedInstanceState)
                initData(mView, savedInstanceState)
            }
        } catch (e: Exception) {
            Logger.e(e)
        }
        return mView
    }

    open fun viewLoad(rootView: View?) {}
    open fun initView(rootView: View?, savedInstanceState: Bundle?) {}
    open fun initData(rootView: View?, savedInstanceState: Bundle?) {}

    fun <T : View> findViewById(id: Int): T {
        return mView!!.findViewById(id)
    }

    override fun onClick(v: View?) {

    }

    fun bindViewClick(vararg views: View) = com.wemei.auto.ui.ext.bindViewClick(this, *views)

}